/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schoolLink;

/**
 *
 * @author Admin
 */
public class Function {   
    public News[] parseNews(String sTemp) {
        //throw new UnsupportedOperationException("Not yet implemented");       
        return null;
    }   
    
    /**
     * String.format ("Your name is %s and you are %s years old", "Mark", "18");
     *
     * @param format
     * @param args
     * @return
     */
    public static String format(String format, String[] args) {
        int argIndex = 0;
        int startOffset = 0;
        int placeholderOffset = format.indexOf("%s");

        if (placeholderOffset == -1) {
            return format;
        }

        int capacity = format.length();

        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                capacity += args[i].length();
            }
        }

        StringBuffer sb = new StringBuffer(capacity);

        while (placeholderOffset != -1) {
            sb.append(format.substring(startOffset, placeholderOffset));

            if (args != null && argIndex < args.length) {
                sb.append(args[argIndex]);
            }

            argIndex++;
            startOffset = placeholderOffset + 2;
            placeholderOffset = format.indexOf("%s", startOffset);
        }

        if (startOffset < format.length()) {
            sb.append(format.substring(startOffset));
        }

        return sb.toString();
    }

    /**
     * Cat chuoi thanh mang
     *
     * @param Source
     * @param Delimiter
     * @return
     */
    public static String[] splitString(String Source, String Delimiter) {
        int iCount, iPos, iLength;
        boolean bEnd;
        String sTemp;
        String[] aSplit = null, t = null;

        sTemp = Source;
        iCount = 0;
        iLength = Delimiter.length();
        bEnd = sTemp.endsWith(Delimiter);

        for (;;) {
            iPos = sTemp.indexOf(Delimiter);
            if (iPos < 0) {
                break;
            } else {
                if (iCount > 0) {
                    t = aSplit;
                }
                iCount++;
                aSplit = new String[iCount];
                if (iCount > 1) {
                    //for (int i = 0; i < t.length; i++) {
                    //    aSplit[i] = t[i];
                    //}
                    System.arraycopy(t, 0, aSplit, 0, t.length);
                }
                aSplit[iCount - 1] = sTemp.substring(0, iPos);
                sTemp = sTemp.substring(iPos + iLength);
            }
        }

        if ((sTemp.length() >= 0) || bEnd) {
            if (iCount > 0) {
                t = aSplit;
            }
            iCount++;
            aSplit = new String[iCount];
            if (iCount > 1) {
                for (int i = 0; i < t.length; i++) {
                    aSplit[i] = t[i];
                }
            }
            aSplit[iCount - 1] = sTemp;
        }
        return aSplit;
    }
}
